package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdsensitiveMapper;
import com.usian.admin.service.AdsensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdsensitiveServiceImpl extends ServiceImpl<AdsensitiveMapper, AdSensitive> implements AdsensitiveService {

    /**
     * 关键字分页查询
     * @param sensitiveDto
     * @return
     */
    @Override
    public ResponseResult findBynameandpage(SensitiveDto sensitiveDto) {

        //判断是否是空
        if(sensitiveDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //校验参数
        sensitiveDto.checkParam();

        //分页详细
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        Page page = new Page(sensitiveDto.getPage(), sensitiveDto.getSize());
        queryWrapper.like(sensitiveDto.getName()!=null,AdSensitive::getSensitives,sensitiveDto.getName());
        Page page1 = page(page, queryWrapper);

        ResponseResult responseResult = new PageResponseResult(sensitiveDto.getPage(),sensitiveDto.getSize(),(int)page1.getTotal());
        responseResult.setData(page1.getRecords());


        return responseResult;
    }

    @Override
    public ResponseResult add(AdSensitive adSensitive) {
        //判断参数是否为空
        if(adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //赋值时间
        adSensitive.setCreatedTime(new Date());
        //完成添加
        boolean save = save(adSensitive);

        //赋值返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {
        //判断是否可以删除
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //完成删除
        boolean b = removeById(id);

        //完成赋值
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult edit(AdSensitive adSensitive) {
        //判断参数是否为空
        if(adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //完成修改
        boolean b = updateById(adSensitive);
        //完成赋值返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
